package com.jvtd.gd_location.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.List;

/*
 * Create by Jack Zhang
 */
public class ForegroundUtil
{
  public static boolean isBackground(Context context)
  {
    ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses)
    {
      if (appProcess.processName.equals(context.getPackageName()))
      {
                /*
                BACKGROUND=400 EMPTY=500 FOREGROUND=100
                GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
                 */
        Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance + ",context.getClass().getName()=" + context.getClass().getName());
        if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
        {
          Log.i(context.getPackageName(), "处于后台" + appProcess.processName);
          return true;
        } else
        {
          Log.i(context.getPackageName(), "处于前台" + appProcess.processName);
          return false;
        }
      }
    }
    return false;
  }
}
